package com.tenbent.product.base.query.page;

import com.tenbent.product.base.bo.BizObject;
import org.apache.commons.collections.map.HashedMap;

import java.io.Serializable;
import java.util.Map;

/**
 * 分页信息
 *
 * @author Randy
 */
public class Pageable implements Serializable {

	private static final long serialVersionUID = -3930180379790344299L;

	/**
	 * 默认页码
	 */
	private static final int DEFAULT_PAGE_NUMBER = 1;

	/**
	 * 默认每页记录数
	 */
	private static final int DEFAULT_PAGE_SIZE = 10;

	/**
	 * 最大每页记录数
	 */
	private static final int MAX_PAGE_SIZE = 1000;

	/**
	 * 每页记录数
	 */
	private Integer pageSize = DEFAULT_PAGE_SIZE;

	/**
	 * 页码
	 */
	private Integer pageNumber = DEFAULT_PAGE_NUMBER;

	/**
	 * 分页起始行（0为第一行）
	 */
	private Integer pageStart = 0;

	/**
	 * 排序属性
	 */
	private String orderProperty;

	/**
	 * 查询参数map
	 */
	private Map<String, Object> paramMap = new HashedMap();

	/**
	 * 查询参数对象
	 */
	private BizObject paramBizObject;

	/**
	 * 默认页
	 *
	 * @return
	 */
	public static Pageable defaultPage() {
		return new Pageable();
	}

	/**
	 * 初始化一个新创建的Pageable对象
	 */
	public Pageable() {
	}

	/**
	 * 初始化一个新创建的Pageable对象
	 *
	 * @param pageNumber
	 *            页码
	 * @param pageSize
	 *            每页记录数
	 */
	public Pageable(Integer pageNumber, Integer pageSize) {
		if (pageNumber != null && pageNumber >= 1) {
			this.pageNumber = pageNumber;
		}
		if (pageSize != null && pageSize >= 1 && pageSize <= MAX_PAGE_SIZE) {
			this.pageSize = pageSize;
		}
	}

	public Pageable(Integer pageNumber, Integer pageSize, Class clazz) {
		if (pageNumber != null && pageNumber >= 1) {
			this.pageNumber = pageNumber;
		}
		if (pageSize != null && pageSize >= 1 && pageSize <= MAX_PAGE_SIZE) {
			this.pageSize = pageSize;
		}
	}

	/**
	 * 获取页码
	 *
	 * @return 页码
	 */
	public int getPageNumber() {
		return pageNumber;
	}

	/**
	 * 设置每页记录数
	 *
	 * @param pageSize
	 *            每页记录数
	 */
	public void setPageSize(Integer pageSize) {
		if (pageSize == null || pageSize < 1 || pageSize > MAX_PAGE_SIZE) {
			pageSize = DEFAULT_PAGE_SIZE;
		}
		this.pageSize = pageSize;

		setPageNumber(pageNumber);
	}

	/**
	 * 设置页码
	 *
	 * @param pageNumber
	 *            页码
	 */
	public void setPageNumber(Integer pageNumber) {
		if (pageNumber == null || pageNumber < 1) {
			pageNumber = DEFAULT_PAGE_NUMBER;
		}
		this.pageNumber = pageNumber;
		this.pageStart = (pageNumber - 1) * pageSize;
	}

	/**
	 * 获取每页记录数
	 *
	 * @return 每页记录数
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 获取分页起始行
	 *
	 * @return 分页起始行
	 */
	public int getPageStart() {
		return pageStart;
	}

	/**
	 * 设置分页起始行
	 *
	 * @param pageStart
	 *            分页起始行
	 */
	public void setPageStart(int pageStart) {
		this.pageStart = pageStart;
	}

	public String getOrderProperty() {
		return orderProperty;
	}

	public void setOrderProperty(String orderProperty) {
		this.orderProperty = orderProperty;
	}

	public Map<String, Object> getParamMap() {
		return paramMap;
	}

	public void setParamMap(Map<String, Object> paramMap) {
		this.paramMap = paramMap;
	}

	public BizObject getParamBizObject() {
		return paramBizObject;
	}

	public void setParamBizObject(BizObject paramBizObject) {
		this.paramBizObject = paramBizObject;
	}

	@Override
	public String toString() {
		return "Pageable{" + "pageSize=" + pageSize + ", pageNumber=" + pageNumber + ", pageStart=" + pageStart
				+ ", orderProperty='" + orderProperty + '\'' + ", paramMap=" + paramMap + '}';
	}
}